package com.cqjtu.eecs.service.impl;

import com.cqjtu.eecs.entity.Course;
import com.cqjtu.eecs.mapper.CourseMapper;
import com.cqjtu.eecs.service.ICourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author   
 * @since 2022-02-28
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {


    @Autowired
    private CourseMapper courseMapper;

    @Override
    public int updateCourseByCourseCode(Integer id, Course course) {
        return courseMapper.updateCourseByCourseCode(id,course);
    }

    @Override
    public List<Course> selectAllCourse(int currPage, int pageSize) {
        //查询全部数据
        ArrayList<Course> courses = courseMapper.selectAllCourse();
        //从第几条数据开始
        int firstIndex = (currPage - 1) * pageSize;
        //到第几条数据结束
        int lastIndex = currPage * pageSize;
        return courses.subList(firstIndex, lastIndex);
    }

    @Override
    public int deletCourseById(Integer id) {
        return courseMapper.deletCourseById(id);
    }

    /**
     * 获取sheet表中所有课程在数据库中的课程名称代码键值对
     * @param names
     * @param majorCode
     * @param versionCode
     * @return
     */
    @Override
    public Map<String, String> listCourseNameCodeMapByNameVersionCodeAndMajorCode(List<String> names, String majorCode, String versionCode) {
        // 将查询出来的map集合转换为map
        try {
            Map<String, String> courseNameCodeMap = new HashMap<>();
            List<Map<String, Object>> courseNameCodeMapList = courseMapper.listCourseNameCodeMapByNameVersionCodeAndMajorCode(names, majorCode, versionCode);
            for (Map<String, Object> map: courseNameCodeMapList) {
                String courseName = null;
                String courseCode = null;

                for (Map.Entry<String, Object> entry: map.entrySet()) {
                    if ("courseName".equals(entry.getKey()))
                        courseName = String.valueOf(entry.getValue());
                    if ("courseCode".equals(entry.getKey()))
                        courseCode = String.valueOf(entry.getValue());
                }
                courseNameCodeMap.put(courseName, courseCode);
            }
            return courseNameCodeMap;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int selectAllCourseSum() {
        return courseMapper.selectAllCourseSum();
    }

    @Override
    public Course selectCourseByCourseName(String courseName) {
        return courseMapper.selectCourseByCourseName(courseName);
    }

    @Override
    public List<Course> selectAllCourselimit(int currPage, int pageSize) {
        return courseMapper.selectAllCourselimit(currPage,pageSize);
    }

    @Override
    public List<Course> selectAllCourseByLike(String courseName) {
        return courseMapper.selectAllCourseByLike(courseName);
    }
}
